IMAGE_SIZE = (854, 480)

def rule_person(result, image_size = IMAGE_SIZE, threshold=0.5):
    if result['name']!='person':
        return True
    h, w = image_size
    low = 0.92*h
    if 'y1' in result['box']:
        y1 = result['box']['y1']
        y2 = result['box']['y2']
    else:
        y1 = result['box'][1]
        y2 = result['box'][3]
    if y2 > low:
        return True
    if (y2 - max(y1, low))/(y2 - y1) > threshold:
        return False
    return True

def rule_object(result, image_size = IMAGE_SIZE, threshold=0.5):
    if result['name']=='person':
        return True
    h, w = image_size
    low2 = 0.95*h
    low1 = 0.4*h
    if 'y1' in result['box']:
        y1 = result['box']['y1']
        y2 = result['box']['y2']
    else:
        y1 = result['box'][1]
        y2 = result['box'][3]   
    if y2 < low1 or y1 > low2:
        return False
    # if (max(x1, low1) - min(x2, low2))/(low2 - low1) > threshold:
    #     return False
    return True

Rules = [rule_person, rule_object]

def filter(origin_results, rules=Rules):
    filter_results = []
    for result in origin_results:
        if all(rule(result) for rule in rules):
            filter_results.append(result)
    return filter_results
